package com.cwd.arm.inter.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwd.arm.application.dto.RoleDto;
import com.cwd.arm.application.service.read.facade.AuthReadService;
import com.cwd.arm.application.service.read.facade.RoleReadService;
import com.cwd.arm.application.service.write.facade.RoleWriteService;
import com.cwd.arm.common.dto.session.AuthDto;
import com.cwd.arm.inter.conf.annotation.ArmAliJsonRequestBody;
import com.cwd.arm.inter.query.RoleQuery;
import com.cwd.arm.inter.util.DtoToVoUtil;
import com.cwd.arm.inter.util.PageDtoToVoUtil;
import com.cwd.arm.inter.util.QueryToDaoQueryUtil;
import com.cwd.arm.inter.util.VoToDtoUtil;
import com.cwd.arm.inter.vo.PageVo;
import com.cwd.arm.inter.vo.ResponseVo;
import com.cwd.arm.inter.vo.auth.AuthVo;
import com.cwd.arm.inter.vo.role.RoleAddVo;
import com.cwd.arm.inter.vo.role.RoleUpdateVo;
import com.cwd.arm.inter.vo.role.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// TODO: 21-12-25 增加角色修改
/**
* Description:角色
* Created on 2021年11月16日
* @author  yan
*/
@Api(value = "角色接口", description = "角色接口")
@RestController
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    private RoleReadService readService;

    @Autowired
    private RoleWriteService roleWriteService;

    @Autowired
    private AuthReadService authReadService;

    @ApiOperation(value = "查询角色信息", notes = "查询角色信息")
    @ApiImplicitParams(value = {
    @ApiImplicitParam(name = "id", value = "角色id",required = true,dataType = "Long", paramType = "path")
    })
    @GetMapping("/{id}")
    public ResponseVo<RoleVo> getRole(@PathVariable Long id) {
        RoleDto dto = readService.selectById(id);
        return ResponseVo.<RoleVo>BUILDER().success(convertToVo(dto));
    }

    @ApiOperation(value = "查询角色列表", notes = "查询角色列表")
    @GetMapping("/page")
    public ResponseVo<PageVo<RoleVo>> getRolePage(RoleQuery query) {
        Page<RoleDto>  dtoPage = readService.selectPage(QueryToDaoQueryUtil.toRoleQuery(query));
        PageVo<RoleVo> pageVo = new PageVo<>();
        pageVo.setRecordList(DtoToVoUtil.convertToRoleVoList(dtoPage.getRecords()));
        return ResponseVo.<PageVo<RoleVo>>BUILDER().success(PageDtoToVoUtil.toVo(dtoPage,pageVo));
    }

    @ApiOperation(value = "查询所有角色列表", notes = "查询所有角色列表")
    @GetMapping("")
    public ResponseVo<List<RoleVo>> getRoleList() {
        List<RoleDto> dtoList = readService.selectList();
        return ResponseVo.<List<RoleVo>>BUILDER().success(DtoToVoUtil.convertToRoleVoList(dtoList));
    }

    @ApiOperation(value = "查询角色权限信息", notes = "查询角色权限信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "角色id",required = true,dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "权限类型:菜单menu;按钮btn",required = true,dataType = "String", paramType = "path")
    })
    @GetMapping("/{id}/auth")
    public ResponseVo<List<AuthVo>> getRoleAuth(@PathVariable Long id,String type) {
        List<AuthDto> authDtoList = authReadService.selectByUserIdAndType(id,type);
        return ResponseVo.<List<AuthVo>>BUILDER().success(DtoToVoUtil.convertToAuthVoList(authDtoList));
    }


    @ApiOperation(value = "新增角色", notes = "新增角色")
    @Secured({"ROLE_ROLE_ADD"})
    @PostMapping("")
    public ResponseVo<String> add(@RequestBody RoleAddVo addVo) {
        roleWriteService.save(VoToDtoUtil.convertToDto(addVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "修改角色", notes = "修改角色")
    @Secured({"ROLE_ROLE_EDIT"})
    @PutMapping("")
    public ResponseVo<String> update(@ArmAliJsonRequestBody RoleUpdateVo updateVo) {
        roleWriteService.update(VoToDtoUtil.convertToDto(updateVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "删除角色", notes = "删除角色")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "角色id",required = true,dataType = "Long", paramType = "path")
    })
    @Secured({"ROLE_ROLE_DEL"})
    @DeleteMapping("/{id}")
    public ResponseVo<String> del(@PathVariable Long id) {
        roleWriteService.del(id);
        return ResponseVo.<String>BUILDER().success();
    }

    private RoleVo convertToVo(RoleDto dto){
        RoleVo vo = new RoleVo();
        BeanUtils.copyProperties(dto,vo);
        return vo;
    }

}
